/*Replicate tables of "The securitization flash flood." 
See ReadMe.pdf which summarizes the list of data files used. 
*/

clear all
set more off
global sandbox "C:\Delete\"
global replication "C:\Users\kanda\OneDrive - Northeastern University\Projects\Securitization\Replication Packet\"

cd $sandbox
*
log using "$replication\logfile_figures", replace

*********************************************************************
/* Figure 1: Growth in repo activity during the pre-crisis period */
*********************************************************************

import delimited "$replication\Data\FRB_Z1.csv", rowrange(7) clear

rename seriesdescription timeperiod
rename moneymarketfunds mmf_repo

twoway (scatteri -50000 2005 -50000 2007, bcolor(gs13) recast(area) leg(off)) (scatteri 214897 2005 214897 2007, bcolor(gs13) recast(area) leg(off)) (connected mmf_repo timeperiod), title("Repo activity in the shadow banking system") subtitle("Bankruptcy Act in 2005 expands the menu of repo collateral") ytitle("USD mn") xlabel(2000(1)2007) ylabel(,format(%9.0fc)) xtitle("") name(figure1, replace)

graph export "$replication\Figures\figure1.png", replace

*********************************************************************
/* Figure 2: Haircuts on private label MBS */
*********************************************************************

u "$replication\Data\triparty_repo_04_06", clear

graph bar repo_haircut_mbs repo_haircut_govt repo_haircut_paper repo_haircut_corporate repo_haircut_other if repo_qtr>177 & repo_qtr<187, over(repo_qtr_label, label(angle(90) labsize(small))) over(text, desc) nofill legend(order(1 "Private Label MBS" 2 "Govt.(agency,UST)" 3 "Commercial Paper" 4 "Corporate" 5 "Other")) title("Evolution of Repo Haircuts by Collateral in Tri-Party Repo") ///
bar(1, color(*1.8)) bar(2, color(*0.3)) bar(3, color(*0.3)) bar(4, color(*0.3)) bar(5, color(*0.3)) bar(6, color(*0.3)) ytitle("Repo Haircut (%)") subtitle("Hand-collected repurchase contracts from MMMF N-Q filings") name(figure2, replace)

graph export "$replication\Figures\figure2.png", replace

ttest repo_haircut_govt, by(post_bapcpa)

ttest repo_haircut_mbs, by(post_bapcpa)

ttest repo_haircut_corporate, by(post_bapcpa)

ttest repo_haircut_paper, by(post_bapcpa)

*********************************************************************
/* Figure 3: Repo borrowings */
*********************************************************************

u "$replication\Data\triparty_repo_04_06", clear

graph bar amount_due_mbs amount_due_govt amount_due_paper ///
amount_due_corporate amount_due_other if repo_qtr>177 & repo_qtr<187, ///
over(repo_qtr_label, label(angle(90) labsize(small))) over(text, desc) nofill ///
legend(order(1 "Private Label MBS" 2 "Govt. (agency,treasuries)" 3 "Commercial Paper" 4 "Corporate" 5 "Other")) title("Evolution of Collateral in Tri-Party Repo") ///
bar(1, color(*1.8)) bar(2, color(*0.3)) bar(3, color(*0.3)) bar(4, color(*0.3)) bar(5, color(*0.3)) bar(6, color(*0.3)) ytitle("Avg. Amount Due ($ mn)") subtitle("Hand-collected repurchase contracts from MMMF N-Q filings") name(figure3, replace)

graph export "$replication\Figures\figure3.png", replace

ttest amount_due_govt, by(post_bapcpa)

ttest amount_due_mbs, by(post_bapcpa)

ttest amount_due_corporate, by(post_bapcpa)

ttest amount_due_paper, by(post_bapcpa)

*********************************************************************
/* Figure A.1: Synthetic DiD Estimation */
*********************************************************************

u "$replication\Data\bank_holding_company_data", clear

tab year, gen(yr_ind)

forvalues i=2002/2008 {
	
	local yrloop = `i' - 2001
	rename yr_ind`yrloop' yr_ind`i'	
	gen t_yr_ind`i' = treated * yr_ind`i'		
}
*

gen synthetic_treatment = 1 if inlist(holding_id,1951350,1039502,1073757,1120754)

replace synthetic_treatment = 0 if treated == 0
drop if synthetic_treatment == .

replace synthetic_treatment = 0 if synthetic_treatment == 1 & year < 2005

sdid pvt_mbs holding_id year synthetic_treatment, vce(bootstrap) reps(100) seed(1192) graph g2_opt(ytitle("Holdings of private label MBS") title("Comparison of Big4 banks (Treated) with synthetic Big4 (Control)") subtitle("Synthetic DiD estimation based on Arkhangelsky et al. (AER, 2021)") text(18 2006 "ATT = 0.85**" " SE = 0.399") legend(label(1 "Big4") label(2 "Synthetic Big4") pos(6))) graph_export(sdid_big4, .png)

graph export "$replication\Figures\figureA1.png", replace

*********************************************************************
/* Figure A.2: Issuance of structured MBS */
*********************************************************************

u "$replication\Data\CMO_issuances", clear

gen str_qtr = string(qofd(settle_date), "%tq")

gen postbapcpa = 0 
replace postbapcpa = 1 if qtr>= 182 /*182 = 2005q3*/
la var postbapcpa "Post-BAPCPA"

label define region 0 "Pre-BAPCPA" 1 "Post-BAPCPA"
label values postbapcpa region

graph bar (sum) OrigMM if year>=2003 & qtr>=175 & year<=2006 & type_id ==4, over(str_qtr, sort(qtr) label(alt)) title("Issuance of opaque mortgage assets by shadow banks") subtitle("Increase in the quarters following a shock to asset liquidity") ytitle("Total Deal Value (mn)") ylabel(,format(%9.0fc)) over(postbapcpa) nofill name(figureA2, replace)

graph export "$replication\Figures\figureA2.png", replace

ttest OrigMM if type_id==4, by(postbapcpa)

*********************************************************************
/* Figure A.3: Intermediation by shadow banks */
*********************************************************************

import excel "$replication\Data\X_17a_15_data.xlsx", clear firstrow cellrange(A1:I9)

rename RepoActivity repoactivity
rename CAGRMBS cagr

scatter cagr repoactivity || lfit cagr repoactivity, title("Repo and pvt. MBS of large broker-dealers") xtitle("Repo (Fraction of total liab; '04)") legend(off) ytitle("% Growth in Private MBS (CAGR, '04-'06)") subtitle("Hand collected from SEC X-17a-5 filings") xlab(,grid) name(figureA3, replace)

graph export "$replication\Figures\figureA3.png", replace

log close